import 'package:flutter/material.dart';
import 'package:flutter_changdian_football/pages/home/local_course/local_course_controller.dart';
import 'package:flutter_changdian_football/routers/app_routes.dart';
import 'package:flutter_changdian_football/utils/pull/pull_smart_refresher.dart';
import 'package:flutter_changdian_football/widget/course/course_list_item.dart';
import 'package:flutter_changdian_football/widget/input/search_input_widget.dart';
import 'package:flutter_changdian_football/widget/screenutil/custom_screenutil.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:get/route_manager.dart';

/// 本地球场
class LocalCourse extends StatelessWidget {
  const LocalCourse({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xffF8FAF9),
      resizeToAvoidBottomInset: false,
      appBar: AppBar(
        centerTitle: true,
        backgroundColor: const Color(0xffF8FAF9),
        elevation: 0,
        title: Text(
          '本地球场',
          style: TextStyle(fontSize: 16.sp, color: const Color(0xff333333)),
        ),
        iconTheme: const IconThemeData(
          color: Color(0xff333333)
        ),
      ),
      body: GetBuilder<LocalCourseController>(
        init: LocalCourseController(),
        builder: (localCourseController) {
          return Column(
            children: [
              SizedBox(height: 10.w,),

              SearchInputWidget(
                hintText: '请输入关键词',
                controller: localCourseController.searchController,
                onSubmitted: () {
                  EasyLoading.show();
                  localCourseController.requestData(localCourseController.controller!);
                },
                isShowClose: localCourseController.searchController.text != '' ? true : false,
                onChange: () {
                  localCourseController.update();
                },
                onClose: () {
                  localCourseController.searchController.clear();
                  localCourseController.update();
                  EasyLoading.show();
                  localCourseController.requestData(localCourseController.controller!);
                },
              ),

              SizedBox(height: 8.w,),

              Expanded(
                child: RefreshWidget<LocalCourseController>(
                  child: ListView.separated(
                    itemCount: localCourseController.nearbyCourtsList.length,
                    physics: const BouncingScrollPhysics(),
                    padding: EdgeInsets.only(left: 17.w, right: 17.w, bottom: 10.w),
                    separatorBuilder: (context, index) {
                      return Container(
                        height: 0.5.w,
                        width: double.infinity,
                        color: const Color(0xffF0F0F0 ),
                      );
                    },
                    itemBuilder: (BuildContext context, int index) {
                      return CourseListItem(
                        name: localCourseController.nearbyCourtsList[index].courtName ?? '',
                        address: localCourseController.nearbyCourtsList[index].courtAddress ?? '',
                        distance: localCourseController.nearbyCourtsList[index].meter == null || localCourseController.nearbyCourtsList[index].meter == '' ?
                         '未知' : localCourseController.nearbyCourtsList[index].meter ?? '未知',
                        image: (localCourseController.nearbyCourtsList[index].images?.length ?? 0) > 0 ? localCourseController.nearbyCourtsList[index].images![0].courtImageUrl ?? '' : '',
                        onTap: () {
                          Get.toNamed(AppRoutes.localCourseDetail, parameters: {
                            'courtId': '${localCourseController.nearbyCourtsList[index].courtId ?? ''}'
                          });
                        },
                      );
                    },
                  ),
                ),
              )
            ],
          );
        }
      ),
    );
  }

}